package com.acxy.ants.core;

import com.acxy.ants.common.kit.StrKit;
import com.acxy.ants.core.annotation.RunConfig;
import com.acxy.ants.core.config.AntsConfig;
import com.acxy.ants.core.config.Services;
import com.acxy.ants.resful.spring.util.ReflectionUtils;
import org.junit.Before;

import java.lang.reflect.Field;

/**
 * 基本测试类，来进行初始化工作
 *
 * @author MrShun
 * @version 1.0
 * @Date 2017-11-02
 */
public class BaseJunitTest {

    @Before
    public void init() {
        Class<?> cls = this.getClass();
        RunConfig config = cls.getAnnotation(RunConfig.class);
        System.out.println(config);
        AntsContext context = null;
        if (config == null)
            context = new AntsContext(new AntsConfig() {
            });
        else {
            try {
                AntsConfig ac = config.value().newInstance();
                context = new AntsContext(ac);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        Field[] fields = cls.getDeclaredFields();
        for (Field field : fields) {
            String clStr = field.getType().getName();
            ReflectionUtils.makeAccessible(field);
            String key = StrKit.lastSubstring(clStr, '.', 2).replace(".", "_");
            Object service = Services.getService(key);
            System.out.println(service);
            try {
                field.set(this, service);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }


    }
}
